//Assassin cloak

concommand.Add("dm_cloak",function(ply,cmd,args)
	if ply:GetNWString("class") == "assassin" and !timer.IsTimer("cooldown") then
		Cloak(ply)
		timer.Create("uncloak"..ply:UniqueID(),ply:GetNWInt("assassinlvl")*3,1,function() Uncloak(ply) end)
		timer.Create("cooldown"..ply:UniqueID(),20,1,function() timer.Remove("cooldown"..ply:UniqueID()) end)
		ply:SendLua("Perk.Cooldown = CurTime() + 20 + LocalPlayer():GetNWInt(\"assassinlvl\")*3")
	else
		ply:ChatPrint("Cloak is charging!")
	end
end)

function Cloak(ply)
	ply:SetColor(255,255,255,25-math.Clamp(ply:GetNWInt("assassinlvl")*3,3,15))
	for k,v in pairs(ply:GetWeapons()) do
		v:SetColor(255,255,255,25-math.Clamp(ply:GetNWInt("assassinlvl")*3,3,15))
	end
end

function Uncloak(ply)
	ply:SetColor(255,255,255,255)
	for k,v in pairs(ply:GetWeapons()) do
		v:SetColor(255,255,255,255)
	end
end

//End cloak


//Marksman radar

concommand.Add("dm_radar",function(ply,cmd,args)
	print("true")
	if ply:GetNWString("class") == "marksman" and !timer.IsTimer("cooldown"..ply:UniqueID()) then
		ply:SetNWBool("markenemies",true)
		timer.Create("stopmark",ply:GetNWInt("marksmanlvl")*3,1,function() ply:SetNWBool("markenemies",false) end)
		timer.Create("cooldown"..ply:UniqueID(),20,1,function() timer.Remove("cooldown"..ply:UniqueID()) end)
		ply:SendLua("Perk.Cooldown = CurTime() + 20 + LocalPlayer():GetNWInt(\"marksmanlvl\")*3")
	else
		ply:ChatPrint("Radar is charging!")
	end
end)

//End radar


//Support barrier

local BarrierOwners = {}
AutoUndo = 0

function CreateBarrier(ply,cmd,args)
	if ply:GetNWString("class") == "support" then
		if !BarrierOwners[ply:SteamID()] then table.Merge(BarrierOwners,{[ply:SteamID()] = 0}) end
		SpawnPos = ply:GetPos() + Vector(ply:GetAimVector().x,ply:GetAimVector().y,0)*100 + Vector(0,0,25)
		PCount = 0
		TRdat = {}
		TRdat.start = SpawnPos
		TRdat.endpos = SpawnPos - Vector(0,0,10000)
		TRdat.filter = player.GetAll()
		
		TRresult = util.TraceLine(TRdat)
		
		if TRresult.HitPos.z < SpawnPos.z and TRresult.HitPos.z > ply:GetPos().z-25 and !TRresult.StartSolid then
			if BarrierOwners[ply:SteamID()]<ply:GetNWInt("supportlvl") and !timer.IsTimer("basecreated") then
				timer.Create("basecreated",5,1,function()
					ply:ChatPrint("Barrier built!")
					BarrierOwners[ply:SteamID()] = BarrierOwners[ply:SteamID()] +1
					print("DM: "..ply:Nick().." now has "..BarrierOwners[ply:SteamID()].." barriers!")
					timer.Remove("basecreated")
				end)
				Base = ents.Create("prop_physics")
				Base:SetModel("models/props_debris/concrete_debris128Pile001a.mdl")
				Base:SetPos(SpawnPos-Vector(0,0,SpawnPos.z-TRresult.HitPos.z))
				Base:SetMoveType(MOVETYPE_VPHYSICS)
				Base:Spawn()
				Base:GetPhysicsObject():SetAngle(FVecToAng(SpawnPos,ply:GetPos()))
				Base:GetPhysicsObject():EnableMotion(false)
				BAng=FVecToAng(SpawnPos,ply:GetPos())
				BPos=SpawnPos-Vector(0,0,SpawnPos.z-TRresult.HitPos.z)
				timer.Create("fin",5,1,function()
					Base:Remove()
					Barrier = ents.Create("prop_physics")
					Barrier:SetModel("models/props_c17/concrete_barrier001a.mdl")
					Barrier:SetPos(BPos)
					Barrier:SetMoveType(MOVETYPE_VPHYSICS)
					Barrier:SetSkin(math.random(1,2))
					Barrier:Spawn()
					if team.GetName(ply:Team()) == "Red Team" then Barrier:SetColor(255,100,100,255) elseif team.GetName(ply:Team()) == "Blue Team" then Barrier:SetColor(100,100,255,255) end
					Barrier:GetPhysicsObject():SetAngle(BAng)
					Barrier:GetPhysicsObject():EnableMotion(false)
					Barrier:SetNWInt("health",ply:GetNWInt("supportlvl")*50)
					Barrier:SetNWEntity("owner",ply)
					Barrier:SetNWString("type","barrier")
					if AutoUndo==1 then
						timer.Create("undo "..Barrier:EntIndex(),300,1,function()
							BarrierOwners[ply:SteamID()] = BarrierOwners[ply:SteamID()] - 1
							if Barrier then Barrier:Remove() end
						end)
					end
				end)
			else
				if BarrierOwners[ply:SteamID()] > ply:GetNWInt("supportlvl") then ply:ChatPrint("Max barriers reached!") end
			end
		end
	else
		ply:ChatPrint("You must be playing as support to build barriers!")
	end
end


AllDebris={}
function Break(ent)
	if timer.IsTimer("undo "..ent:EntIndex()) then
		timer.Remove("undo "..ent:EntIndex())
	end
	if ent:IsValid() then
		TRdat = {}
		TRdat.start = ent:GetPos()
		TRdat.endpos = ent:GetPos() - Vector(0,0,10000)
		TRdat.filter = {ent}
		
		TRresult = util.TraceLine(TRdat)
		
		Debris = ents.Create("prop_physics")
		Debris:SetModel("models/props_debris/concrete_debris128Pile001a.mdl")
		Debris:SetPos(ent:GetPos()-Vector(0,0,ent:GetPos().z-TRresult.HitPos.z))
		Debris:SetMoveType(MOVETYPE_NONE)
		Debris:Spawn()
		Debris:GetPhysicsObject():SetAngle(Angle(0,ent:GetAngles().yaw,0))
		Debris:GetPhysicsObject():EnableMotion(false)
		table.insert(AllDebris,Debris)
		timer.Create("clean"..Debris:EntIndex(),300,1,function()
			print("DM: Removing debree ["..AllDebris[1]:EntIndex().."]")
			if AllDebris[1]:IsValid() then table.remove(AllDebris,1):Remove() end
		end)
	end
end


function FVecToAng( vec1, vec2 )
	local ang = ( vec2 - vec1 ):Angle()
	return Angle(0,ang.yaw,0)
end

concommand.Add("dm_barrier",CreateBarrier)

concommand.Add("dm_autoundo", function(ply,cmd,args)
	if ply:IsAdmin() and args[1] then AutoUndo = args[1] end
	print("AutoUndo set to "..AutoUndo)
end)

//End barrier

function Checkhealth(ent,wep,attacker,dmg,dmginfo)
	if ent:GetNWInt("health") then
		ent:SetNWInt("health",ent:GetNWInt("health")-dmg)
	end
	if ent:GetNWInt("health") < 1 then
		if !ent:GetPhysicsObject():IsMoveable() then
			ent:GetPhysicsObject():EnableMotion(true)
			print("DM: "..attacker:Nick().." destroyed "..ent:GetNWEntity("owner"):Nick().."'s "..ent:GetNWString("type"))
			attacker:ChatPrint("You destroyed "..ent:GetNWEntity("owner"):Nick().."'s "..ent:GetNWString("type"))
			ent:GetNWEntity("owner"):ChatPrint(attacker:Nick().." destroyed your "..ent:GetNWString("type"))
		end
		ent:SetNWInt("disp",0.5)
		if !timer.IsTimer("killent"..ent:EntIndex()) then
			timer.Create("killent"..ent:EntIndex(),30,1,function()
				print("DM: Replacing entity ["..ent:EntIndex().."] with debree")
				Break(ent)
				if ent:GetNWEntity("owner"):IsValid() then
					if ent:GetModel()=="models/props_c17/concrete_barrier001a.mdl" then BarrierOwners[ent:GetNWEntity("owner"):SteamID()] = BarrierOwners[ent:GetNWEntity("owner"):SteamID()]-1 end
					if ent:GetNWInt("disp") == 0.5 then
						ent:GetNWEntity("owner"):SetNWInt("disp",0)
					end
				else
					for k,v in pairs(BarrierOwners) do
						PlFind = false
						for i,p in pairs(player.GetAll()) do
							if p:SteamID() == k then PlFind = true end
						end
						if !PlFind then BarrierOwners[k] = 0 end
					end
					ent:GetNWEntity("owner"):SetNWInt("disp",0)
				end
				timer.Remove("killent"..ent:EntIndex())
				ent:Remove()
			end)
		end
	end
end

//Support dispenser

function BuildDispenser(ply,cmd,args)
	if ply:GetNWInt("disp") == 0 then
		DSpawnPos = ply:GetPos() + Vector(ply:GetAimVector().x,ply:GetAimVector().y,0)*100 + Vector(0,0,25)
		TRdat = {}
		TRdat.start = DSpawnPos+Vector(0,0,25)
		TRdat.endpos = DSpawnPos - Vector(0,0,10000)
		TRdat.filter = player.GetAll()
		
		TRresult = util.TraceLine(TRdat)
		
		if TRresult.HitPos.z < DSpawnPos.z and TRresult.HitPos.z > ply:GetPos().z-25 and !TRresult.StartSolid then
			DPos = DSpawnPos-Vector(0,0,DSpawnPos.z-TRresult.HitPos.z-35)
			DAng = FVecToAng(DSpawnPos,ply:GetPos())
			Disp = ents.Create("prop_physics")
			Disp:SetModel("models/props_lab/reciever_cart.mdl")
			Disp:SetPos(DPos)
			Disp:SetMoveType(MOVETYPE_VPHYSICS)
			Disp:Spawn()
			if team.GetName(ply:Team()) == "Red Team" then Disp:SetColor(255,100,100,255) elseif team.GetName(ply:Team()) == "Blue Team" then Disp:SetColor(100,100,255,255) end
			Disp:SetAngles(DAng)
			Disp:GetPhysicsObject():EnableMotion(false)
			Disp:SetNWEntity("owner",ply)
			Disp:SetNWInt("health",50)
			Disp:SetNWInt("disp",1)
			Disp:SetNWString("type","dispenser")
			ply:SetNWInt("disp",1)
		end
	end
end

function Resupply(ply,key)
	if key == IN_USE and ply:IsValid() then
		local ETrace=ply:GetEyeTrace()
		if ETrace.Entity:IsValid() then
			if ETrace.Entity:GetNWInt("disp") == 1 and !timer.IsTimer(ply:SteamID().."dispcool") and ETrace.Entity:GetNWEntity("owner"):Team()==ply:Team() then
				if ply:GetNWString("class") == "soldier" then
					ply:SetHealth(math.Clamp((ply:Health()+ETrace.Entity:GetNWEntity("owner"):GetNWInt("supportlvl")*20),1,100))
					ply:SetArmor(math.Clamp((ply:Armor()+ETrace.Entity:GetNWEntity("owner"):GetNWInt("supportlvl")*20),1,100))
				elseif ply:GetNWString("class") == "support" then
					ply:SetHealth(math.Clamp((ply:Health()+ETrace.Entity:GetNWEntity("owner"):GetNWInt("supportlvl")*20),1,100))
				elseif ply:GetNWString("class") == "assassin" then
					ply:SetHealth(math.Clamp((ply:Health()+ETrace.Entity:GetNWEntity("owner"):GetNWInt("supportlvl")*20),1,50))
				elseif ply:GetNWString("class") == "marksman" then
					ply:SetHealth(math.Clamp((ply:Health()+ETrace.Entity:GetNWEntity("owner"):GetNWInt("supportlvl")*20),1,100))
				end
				if ply:GetActiveWeapon():GetPrimaryAmmoType()!="grenade" and ply:GetActiveWeapon():GetPrimaryAmmoType()!="xbowbolt" then
					ply:GiveAmmo(30,ply:GetActiveWeapon():GetPrimaryAmmoType())
				end
				if ETrace.Entity:GetNWEntity("owner") != ply then
					ETrace.Entity:GetNWEntity("owner"):ChatPrint(ply:Nick().." Has used your dispenser!")
					ply:ChatPrint("You used "..ETrace.Entity:GetNWEntity("owner"):Nick().."'s dispenser")
				end
				print("DM: "..ply:Nick().." is on a dispenser cooldown")
				timer.Create(ply:SteamID().."dispcool",10,1,function()
					print("DM: "..ply:Nick().."'s dispenser cooldown has ended")
					timer.Remove(ply:SteamID().."dispcool")
				end)
			end
		end
	end
end

concommand.Add("dm_disp",BuildDispenser)
hook.Add("KeyPress","DispUse",Resupply)

//End dispenser


//Soldier crit mode

concommand.Add("dm_crit",function(ply,cmd,args)
	if ply:GetNWString("class") == "soldier" then
		ply:SetNWInt("crit",1)
		timer.Create(ply:SteamID().."crit",ply:GetNWInt("soldierlvl")*5,1,function()
			ply:SetNWInt("crit",0)
		end)
	end
end)

//End crit mode